<?php
/*
<!-- HTML FOR TESTING THE POST COMMAND -->
<html>
	<form action="getfriends.php" method="post">
	UserId: <input type="text" name="id" />
	<input type="submit" />Users
	</form>
</html>
*/
?>
<?php

	function getUser($id){
		$query = "SELECT * FROM  `Users` WHERE `id`='".$id."'";
		$result = mysql_query($query);
		checkResult($result);
		$row = mysql_fetch_assoc($result);
		$user = array(
			"name" => $row["Name"],
			"playing" => $row["CurrentlyPlaying"],
			"ip" => $row["ip"]
		);
		return $user;
	}

	function checkResult($result){
		if(!$result){
	    		die($message);
		}
	}

	if ($_SERVER['REQUEST_METHOD'] == 'POST') {
	
		mysql_connect("localhost","akosciak","MojaPtaszek");
		
		mysql_select_db("songbump") or die( "Unable to select database");
	
		$id = $_POST["id"];
		include("queries.php");
		$query = getNeighborsQuery($id);
		$result = mysql_query($query);
		checkResult($result);

		// User Results
		$friends = array();
		while ($row = mysql_fetch_assoc($result)) {
			$user = getUser($row["Neighbors"]);
			$friend = array(
				"name" => $user["name"],
				"playing" => $user["playing"],
				"id" => $row["Neighbors"]
			);
			$friends[] = $friend;
		}
		
		mysql_free_result($result);
			
		$friendsJSON = json_encode($friends);
		echo $friendsJSON;
		
	}
	
?>
